package com.example.wwk.graduationpractice.NetDataUtils;

import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.example.wwk.graduationpractice.Bean.AnswerBean;
import com.example.wwk.graduationpractice.Bean.MaxQuestionIdBean;
import com.example.wwk.graduationpractice.Bean.QuestionBean;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.util.List;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by wwk on 2017/4/19.
 * 网络请求。获取动态评论需要的数据
 */

public class DynamicDataUtils {
    public static final int GET_QUESTION_SUCCESS = 100;
    public static final int GET_ANSWER_SUCCESS = 101;
    public static final int GET_MAXQUESTIONID_SUCCESS = 102;

    public static void getQuestions(final Handler handler){
        String Url = "http://192.168.191.1:9090/plugins/dynamic/getdynamic?action=queryquestion";
        final String finalUrl = Url;
        new Thread(new Runnable() {
            @Override
            public void run() {
                List<QuestionBean> questionBeen ;
                OkHttpClient client = new OkHttpClient();
                Request request = new Request.Builder()
                        .url(finalUrl)
                        .build();
                try {
                    Response response = client.newCall(request).execute();
                    String responseData = response.body().string();
                    Log.d("123", "getQuestions run: "+responseData);
                    Gson gson = new Gson();
                    questionBeen = gson.fromJson(responseData, new TypeToken<List<QuestionBean>>() {
                    }.getType());

                    Message msg=handler.obtainMessage(GET_QUESTION_SUCCESS,questionBeen);
                    msg.sendToTarget();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }).start();
    }
    public static void getAnwers(String questionid,final Handler handler){
        String Url = "http://192.168.191.1:9090/plugins/dynamic/getdynamic?action=queryanswerofthequestion";
        Url+="&questionid="+questionid;
        final String finalUrl = Url;
        new Thread(new Runnable() {
            @Override
            public void run() {
                List<AnswerBean> answerBeen ;
                OkHttpClient client = new OkHttpClient();
                Request request = new Request.Builder()
                        .url(finalUrl)
                        .build();
                try {
                    Response response = client.newCall(request).execute();
                    String responseData = response.body().string();
                    Gson gson = new Gson();
                    answerBeen = gson.fromJson(responseData, new TypeToken<List<AnswerBean>>() {
                    }.getType());
                    Log.d("123", "getAnwers run: "+responseData);
                    Message msg=handler.obtainMessage(GET_ANSWER_SUCCESS,answerBeen);
                    msg.sendToTarget();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }).start();
    }
    public static void getMaxQuestionId(final Handler handler){
        String Url = "http://192.168.191.1:9090/plugins/dynamic/getdynamic?action=querymaxquestionId";
        final String finalUrl = Url;
        new Thread(new Runnable() {
            @Override
            public void run() {
                OkHttpClient client = new OkHttpClient();
                Request request = new Request.Builder()
                        .url(finalUrl)
                        .build();
                try {
                    MaxQuestionIdBean maxQuestionIdBean;
                    Response response = client.newCall(request).execute();
                    String responseData = response.body().string();
                    Log.d("123", "getMaxQuestionId run: "+responseData);
                    Gson gson = new Gson();
                    maxQuestionIdBean = gson.fromJson(responseData, new TypeToken<MaxQuestionIdBean>() {
                    }.getType());

                    Message msg=handler.obtainMessage(GET_MAXQUESTIONID_SUCCESS,maxQuestionIdBean);
                    msg.sendToTarget();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }).start();
    }
}
